﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace StudentManagement
{
    public partial class frmKecheng : Form
    {
        public frmKecheng()
        {
            InitializeComponent();
        }
        private StudentInfo studentInfo;

        public StudentInfo StudentInfo
        {
            get { return studentInfo; }
            set { studentInfo = value; }
        }

        
        private void btnadd_Click(object sender, EventArgs e)
        {
            if (studentInfo != null)
            {
                

                studentInfo.Id = this.txtid.Text;
                studentInfo.Name = this.txtname.Text;
                string connstr = "server=.;database=Class;user=sa;password=123456";
                SqlConnection conn = new SqlConnection(connstr);
                string updateSql = "UPDATE [subject] SET [name] = '" + studentInfo.Name+ "' where id='" + studentInfo.Id + "'";
                SqlCommand comm = new SqlCommand(updateSql, conn);
                conn.Open();
                int count = comm.ExecuteNonQuery();
                if (count > 0)
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
            else
            {
            StudentInfo s = new StudentInfo();
            s.Id = this.txtid.Text;
            s.Name = this.txtname.Text;
            string connstr = "server=.;database=Class;user=sa;password=123456";
            SqlConnection conn = new SqlConnection(connstr);
            string insertSql = "insert into Subject values('" + s.Id + "','" + s.Name + "') ";
            SqlCommand comm = new SqlCommand(insertSql, conn);
            conn.Open();
            int count = comm.ExecuteNonQuery();
            conn.Close();
            if (count > 0)
            {
                this.DialogResult = DialogResult.OK;
            }
            }
        }

        private void btnquxiao_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void frmKecheng_Load(object sender, EventArgs e)
        {
            if (studentInfo != null)
            {
                this.txtid.Text = studentInfo.Id;
                this.txtname.Text = studentInfo.Name;
            }
        }

        

   
    }
}
